//
//  VisitorView.swift
//  上海微博
//
//  Created by 🍃叶子 on 16/4/18.
//  Copyright © 2016年 🍃叶子. All rights reserved.
//

/**
用存代码开发的步骤是啥:
1. 先把界面要求的元素准备出来
2. 然后再搭建界面
*/


import UIKit
import SnapKit

/// 访客视图
class VisitorView: UIView {
    
    // 默认构造函数是initwithFrame
    /// MARK: - 生命周期函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    // 用StoryBoard开发的时候不能使用这个类 -->  _(:зゝ∠)_刀哥说的
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 公共方法
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图像名,首页默认为nil
    /// - parameter message:   提示文字
   func setupInfo(imageName:String? = nil,message:String) {
        messageLabel.text = message
    //如果图像为nil,表示为首页
        guard let imageName = imageName else {
            starAnimation()
            return
        }
        // 不是首页需要隐藏房子
        homeIconView.hidden = true
        coverView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    
    /// 开始动画
    private func starAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        // 提示:对于需要循环播放的动画,可以设置此属性为 false
        // 视图离开屏幕后,不会被销毁! 当动画所在视图被销毁后,动画会连带销毁
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 私有控件
    /// 图标视图
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子图标
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house") )
  
    /// 消息标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        //        label.tintColor = UIColor.orangeColor()
        return label
    }()
    /// 注册标签
    private lazy var registerButton: UIButton = {
       let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    /// 登录标签
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    
}

// MARK: - 设置界面
extension VisitorView {
    
    private func setupUI() {
        
        // 0.
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        // 养成好习惯,不然会吃亏的
        
        // 1. 添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 自动布局
        // 1> 参照属性一致,可以省略
        // 2> 参数属性不一致,使用snp_参照属性的格式
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        coverView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        
        // trailing 和 left 的区别
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.left.equalTo(messageLabel)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
        }
       
        // 空白颜色:只要能设置颜色的不要设置图片
        
    }
    
    
    
    
    
    
    
    
    
    
    
}



/*

// 2.自动布局
for v in subviews {
v.translatesAutoresizingMaskIntoConstraints = false
}

// VFL - 可视话格式语言,设置居中非常麻烦
// 苹果原生的自动布局
/**
核心公式:view1.attr1 = view2.attr2 * multiplier + constant
一个视图.属性 = 另一个视图.属性 * 乘积 + 约束值

约束是添加到公共的父控件上

* 使用自动布局时,尽量参照唯一一个控件进行布局,不要把所有约束都设置到父视图参照
- 好处: 如果需要修改,只需要改动一个其他的跟着也改了
*/

// 图标视图
self.addConstraint(NSLayoutConstraint(item: iconView,
attribute: .CenterX,
relatedBy: .Equal,
toItem: self,
attribute: .CenterX,
multiplier: 1.0,
constant: 0))
self.addConstraint(NSLayoutConstraint(item: iconView,
attribute: .CenterY,
relatedBy: .Equal,
toItem: self,
attribute: .CenterY,
multiplier: 1.0,
constant: -60))
// 小房子视图
self.addConstraint(NSLayoutConstraint(item: homeIconView,
attribute: .CenterX,
relatedBy: .Equal,
toItem: iconView,
attribute: .CenterX,
multiplier: 1.0,
constant: 0))
self.addConstraint(NSLayoutConstraint(item: homeIconView,
attribute: .CenterY,
relatedBy: .Equal,
toItem: iconView,
attribute: .CenterY,
multiplier: 1.0,
constant: 0))
// 提示文字
self.addConstraint(NSLayoutConstraint(item: messageLabel,
attribute: .CenterX,
relatedBy: .Equal,
toItem: iconView,
attribute: .CenterX,
multiplier: 1.0,
constant: 0))
self.addConstraint(NSLayoutConstraint(item: messageLabel,
attribute: .Top,
relatedBy: .Equal,
toItem: iconView,
attribute: .Bottom,
multiplier: 1.0,
constant: 20))
// If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
self.addConstraint(NSLayoutConstraint(item: messageLabel,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1.0, constant: 224))


*/